import org.junit.Test;

import java.util.Arrays;

/**
 * 练习一：模拟一个trim方法，去除字符串两端的空格
 * 练习二：将一个字符串进行反转，将字符串中指定部分进行反转
 * 比如："abcdefg" 反转后"abfedcg"
 * 练习三：获取一个字符串在另一个字符串中出现的次数：
 * 比如:获取"ab"在"abkkcadkabkebfkabkskab"中出现的次数
 * 练习四:获取两个字符串最大相同子串
 *str1 = "abcwerthelloyuiodef“;str2 = "cvhellobnm"//10
 *    提示：将短的那个串进行长度依次递减的子串与较长
 *    的串比较。
 * 5.对字符串中字符进行自然顺序排序。"abcwerthelloyuiodef"
 * 提示：
 * 1）字符串变成字符数组。
 * 2）对数组排序，选择，冒泡，Arrays.sort(str.toCharArray());
 * 3）将排序后的数组变成字符串。
 *
 *
 * @author wx
 * @create 2019-12-12 14:33
 */
public class StringExer {
    public static void main(String[] args) {
        trimDemo1("   a bas  d          ");
        trimDemo2("   a bas d          ");
        reverseDemo("abcdkkbefg",0,10);
        strAppearTime("ab","cdabkkcadkabkebfkabkskab");
        maxSubStr("abcwerthelloyuiodef","cvhellobnm");

    }
    private static void maxSubStr(String mainStr, String subStr) {
        boolean flag = false;
        String resStr = null;
        int index=0;
        for (int lengthFlag = 1; lengthFlag < subStr.length(); lengthFlag++) {
            flag = false;
            index = 0;
            while (index<=subStr.length()){//该长度的子串匹配到了
                index++;
                if(index+lengthFlag<=subStr.length() && mainStr.indexOf(subStr.substring(index,index+lengthFlag)) != -1){
                    resStr=subStr.substring(index,index+lengthFlag);
                    flag = true;
                    break;
                }

            }
            if (flag == false){//全部子串都不匹配
                //输出当前的lengthFlag 和resStr
                System.out.println("lengthFlag = " + (lengthFlag-1)+" resStr: "+ resStr);
                break;
            }
        }
    }

    private static void strAppearTime(String substr, String mainstr) {
        int count = 0;
        int index = 0;
        while ((index =mainstr.indexOf(substr)) != -1){
            count++;
            mainstr=mainstr.substring(index+substr.length());
        }

        //改进:
//        while ((index =mainstr.indexOf(substr,index)) != -1){
//            index +=substr.length();
//            count++;
//        }
        System.out.println("count = " + count);

    }

    //练习
    static void trimDemo1(String s) {
        long starttime = System.currentTimeMillis();
        String resStr = s;

        //去除前面的空格
        for (int i = 0; i < resStr.length() - 1; i++) {
            if (resStr.charAt(i) == ' ') {
                resStr = resStr.substring(i + 1);
            } else break;
        }

        //去除后面的空格
        for (int i = resStr.length() - 1; i >= 0; i--) {
            if (resStr.charAt(i) == ' ') {
                resStr = resStr.substring(0, i);
            } else break;
        }
        System.out.println(resStr);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis() - starttime + "ms");
    }

    static void trimDemo2(String s) {
        long starttime = System.currentTimeMillis();
        int endFlag = s.length();
        int startFlag = 0;
        for (int i = 0; i < endFlag; i++) {
            if (s.charAt(i) != ' ') {
                startFlag = i;
                break;
            }
        }

        for (int i = endFlag - 1; i > 0; i--) {
            if (s.charAt(i) != ' ' && i > startFlag) {
                endFlag = i;
                break;
            }
        }

        System.out.println(s.substring(startFlag, endFlag + 1));
        System.out.println(System.currentTimeMillis() - starttime + "ms");
    }

    static void reverseDemo(String s,int startIndex,int endIndex){
        char[] chars = s.substring(startIndex, endIndex).toCharArray();

//        for (char aChar : chars) {
//            System.out.print(aChar);
//        }
        System.out.println();
//        char[] newChar = new char[chars.length];
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i <=chars.length-1; i++) {
//            newChar[i] = chars[(chars.length-1)-i];
            sb.append(chars[(chars.length-1)-i]);
        }
//        System.out.println("sb = " + sb);
        System.out.println(s.replace(s.substring(startIndex, endIndex), sb.toString()));

    }

    @Test
    public void test1(){
        System.out.println("cvhellobnm".substring(1,2));
        char[] chars = "abcwerthelloyuiodef".toCharArray();
        Arrays.sort(chars);
        System.out.println();
        for (char aChar : chars) {
            System.out.print(aChar);
        }

    }


}
